package com.company;

import java.util.Scanner;

public class SelectOp {

    Items items1 = new Items();
    Items items2 = new Items();


    public SelectOp() {
        items1.add(new Book("Head First Java", "Kathy Sierra", 2002, true));
        items1.add(new Book("The Complete Reference", "Herbert Schildt", 2001, true));
        items1.add(new Book("Let Us C", "Yashavant P. Kanetkar", 1992, true));


        items2.add(new Movie("Kuch Kuch Hota Hai", 1998, "Karan Johar", 8, true));
        items2.add(new Movie("Lagaan", 2001, "Ashutosh Gowariker", 10, true));
        items2.add(new Movie("Kahaani", 2012, "Sujoy Ghosh", 9, true));
    }

    public void selectOperation(String libraryNo) {


        while(true) {
            System.out.println(" ");
            System.out.println("Select an operation");
            System.out.println(" ");
            System.out.println("1.Show User Information");
            System.out.println("2.List all books in library");
            System.out.println("3.List Available books");
            System.out.println("4.CheckOut A Book");
            System.out.println("5.Return A Book");
            System.out.println("6.List all Movies in library");
            System.out.println("7.List Available Movies");
            System.out.println("8.CheckOut A Movie");
            System.out.println("9.Return A Movie");
            System.out.println("10.Logout");
            System.out.println("11.Quit");
            System.out.println(" ");

            int ch;
            Scanner in = new Scanner(System.in);
            ch = in.nextInt();
            switch (ch) {
                case 1:
                    Login login = new Login();
                    login.displayCredentials(login.getLibraryId(libraryNo));
                    break;
                case 2:
                    listHeadingForBook();
                    items1.listAllItems();
                    break;
                case 3:
                    listHeadingForBook();
                    items1.listAvilableItems();
                    break;
                case 4:
                    String bookNameToBeCheckedout = checkoutInput("book");
                    boolean checkoutBookResult = items1.checkoutItem(libraryNo, bookNameToBeCheckedout);
                    checkoutOutput(checkoutBookResult, "book");
                    break;
                case 5:
                    String bookNameToBeReturned = returnInput("book");
                    boolean returnBookResult = items1.returnItem(libraryNo, bookNameToBeReturned);
                    returnOutput(returnBookResult, "book");
                    break;
                case 6:
                    listHeadingForMovie();
                    items2.listAllItems();
                    break;
                case 7:
                    listHeadingForMovie();
                    items2.listAvilableItems();
                    break;
                case 8:
                    String movieName = checkoutInput("movie");
                    boolean movieCheckoutResult = items2.checkoutItem(libraryNo, movieName);
                    checkoutOutput(movieCheckoutResult, "movie");
                    break;
                case 9:
                    String movieNameToBeReturned = returnInput("movie");
                    boolean returnMovieResult = items2.returnItem(libraryNo, movieNameToBeReturned);
                    returnOutput(returnMovieResult, "movie");
                    break;
                case 10:
                    return;
                case 11:
                    throw new RuntimeException("You have quit the application");

                default:
                    System.out.println("Select a valid option!");
            }
        }
    }

    public String checkoutInput(String item) {
        System.out.println("Select the name of the " + item + " to be Checked Out");
        Scanner in = new Scanner(System.in);
        String bookName = in.nextLine();
        return bookName.toLowerCase();
    }

    public void checkoutOutput(boolean result, String item) {
        if (result) {
            System.out.println("Thank you! Enjoy the " + item);
        } else {
            System.out.println("That " + item + " is nt available");
        }
    }

    public String returnInput(String item) {
        System.out.println("Enter the name of the " + item + " to be returned");
        Scanner in = new Scanner(System.in);
        String bookName = in.nextLine();
        return bookName.toLowerCase();

    }

    public void returnOutput(boolean result, String item) {
        if (result) {
            System.out.println("Thank you for returning the " + item);
        } else {
            System.out.println("This " + item + " Cant be returned");
        }
    }

    public void listHeadingForBook() {
        System.out.format("%-10s%-30s%-30s%-30s", "SlNo", "Name of Book", "Author", "Year of Publication");
        System.out.println("");
        System.out.println("------------------------------------------------------------------------------------------");
    }

    public void listHeadingForMovie() {
        System.out.format("%-10s%-30s%-30s%-30s%-30s", "SlNo", "Name of Movie", "Year of Release", "Director", "Movie Rating");
        System.out.println("");
        System.out.println("-----------------------------------------------------------------------------------------------------------------");
    }


}

